package com.yycx.module.bus.provider.handler.haiying.tasktodo.askamount;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusPaymentOrderService;
import com.yycx.module.system.provider.service.SysBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 我的已办任务列表
 *
 * @author zyf
 */
@Component
public class HyAskAmountTaskTab2Handler implements PageInterceptor<BusPayment> {


    @Resource
    private SysBankService bankService;

    @Resource
    private BaseUserService userService;

    @Resource
    private BusSalemanService salemanService;

    @Resource
    private BusDeptService deptService;

    @Resource
    private BusCustomService customService;


    @Autowired
    private FlowableHelp flowableHelp;


    @Autowired
    private BusPaymentOrderService paymentOrderService;

    @Autowired
    private BusBankStatementService bankStatementService;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String createBeginDate = cq.getParams("createBeginDate");
        String createEndDate = cq.getParams("createEndDate");
        String amountType = cq.getParams("amountType");
        String customId = cq.getParams("customId");
        String customName = cq.getParams("customName");
        cq.eq(BusPayment.class, "deptId");
        cq.eq(BusPayment.class, "companyId");
        cq.like(BusPayment.class, "paymentNo");
        if (FlymeUtils.isNotEmpty(customName)) {
            String pinyin = PinyinUtil.getFirstLetter(customName, "").toUpperCase();
            cq.and(e -> e.like("customName", customName).or().likeRight("customNamePinYin", pinyin));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("amountDate", beginDate);
            cq.le("amountDate", endDate);
        }
        if (FlymeUtils.isNotEmpty(createBeginDate)) {
            cq.ge("payment.createTime", createBeginDate);
            cq.le("payment.createTime", createEndDate + " 23:59:59");
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            cq.eq("customId", customId);
        }
        //办理中
        cq.in(true, "paymentState", 1);
        if (FlymeUtils.isNotEmpty(amountType)) {
            cq.eq("amountType", amountType);
        } else {
            cq.in(true, "amountType", AmountTypeEnum.YfkAmount.getCode(), AmountTypeEnum.CustomBond.getCode(), AmountTypeEnum.CustomRefund.getCode(),AmountTypeEnum.DepositAmount.getCode());
        }
        cq.eq(true, "advanceState", 1);
        String userName = OpenHelper.getUserName();
        //已审批中有我的记录
        if (!userName.contains("admin")) {
            Long userId = OpenHelper.getUserId();
            cq.apply("'"+userId+"' in(select USER_ID_ from act_hi_comment c where c.PROC_INST_ID_=payment.processInstanceId and c.TYPE_='WC' and c.ACTION_ ='AddComment' and c.USER_ID_='"+userId+"')");
        }
        cq.eq(true, "payment.companyId", OpenHelper.getCompanyId());
        cq.createJoin(BusCustom.class);
        cq.orderByDesc("createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long paymentId = entityMap.getLong("paymentId");
            EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
            if (FlymeUtils.isNotEmpty(taskMap)) {
                entityMap.putAll(taskMap);
            }
            Integer paymentState = entityMap.getInt("paymentState");
            Long saleManId = entityMap.getLong("saleManId");
            if (FlymeUtils.isNotEmpty(saleManId)) {
                BusSaleman saleMan = salemanService.getById(saleManId);
                entityMap.put("saleManName", saleMan.getSaleManName());
                entityMap.put("businessCode", saleMan.getSaleManBusinessNo());
            }
            //部门信息
            Long deptId = entityMap.getLong("deptId");
            BusDept dept = deptService.getById(deptId);
            if (FlymeUtils.isNotEmpty(dept)) {
                entityMap.put("deptName", dept.getDeptName());
                entityMap.put("deptType", dept.getDeptType());
            }
            //剩余金额
            BigDecimal amount = entityMap.getBigDecimal("amount");
            BigDecimal useAmount = paymentOrderService.totalByPaymentId(paymentId);
            BigDecimal surplusAmount = amount.subtract(useAmount);
            entityMap.put("surplusAmount", surplusAmount);
            if (paymentState.equals(2)) {
                entityMap.put("taskName", "已认款");
            }

        }
        return result;
    }

}
